package com.linkai.controller.admin.info;

import com.linkai.controller.admin.BaseController;
import com.linkai.dto.AdminBproductDto;
import com.linkai.dto.AdminBproductGetDto;
import com.linkai.dto.AdminBproductIndexDto;
import com.linkai.entity.Bproduct;
import com.linkai.enums.ResultDTO;
import com.linkai.service.BproSizeService;
import com.linkai.service.BproductService;
import com.linkai.service.SizeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author yamon
 * @Date 2020-12-01 20:40
 * @Description
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin/info/product")
public class ProductInfoController extends BaseController {

    @Autowired
    private BproductService bproductService;

    @Autowired
    private SizeService sizeService;

    @Autowired
    private BproSizeService bproSizeService;

    /**
     * 首页
     *
     * @return model
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        boolean flag = bproductService.isExist();
        final List<AdminBproductIndexDto> bproductList = bproductService.listAllProduct4AdminIndex();
        modelAndView.addObject("flag", flag);
        modelAndView.addObject("products", bproductList);
        modelAndView.setViewName("admin/info/product/index");
        return modelAndView;
    }


    /**
     * 跳转到添加商品页 面
     *
     * @return view
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/info/product/add");
        return modelAndView;
    }

    /**
     * 保存一个商品到数据库中
     *
     * @return flag
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Boolean> save(@RequestBody AdminBproductDto adminBproductDto) {
        final Bproduct bproduct = new Bproduct();
        BeanUtils.copyProperties(adminBproductDto, bproduct);
        if (bproduct.getBproId() != null) {
            return super.updateInfo(bproductService.updateById(bproduct));
        } else {
            return super.successSaveInfo(bproductService.save(bproduct));
        }

    }

    /**
     * 根据id得到商品信息
     *
     * @param id id
     * @return 商品信息
     */
    @GetMapping("/getOneBproduct/{id}")
    @ResponseBody
    public ModelAndView getOneAdminBackProduct(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        final AdminBproductGetDto oneAdminBackProduct = bproductService.getOneAdminBackProduct(id);
        modelAndView.addObject("product", oneAdminBackProduct);
        modelAndView.setViewName("admin/info/product/modify/update");
        return modelAndView;
    }

    /**
     * 删除一个商品
     * @param id id
     * @return model
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultDTO<Boolean> deleteOneBackProduct(@PathVariable Integer id){
        return super.successDeleteInfo(bproductService.removeById(id));
    }



}
